<?php
namespace app\wechat\controller;
use app\api\service\Order;
use think\View;
use think\Db;
use Jssdk\JSSDK;

class Austin extends WCommon
{
    // 申请服务单售后首页
    public function order_server()
    {
        $member_id = session('member_id');  // 用户ID
        $product_id = input('param.product_id', 0);   // 订单ID
        $audit_type = input('param.audit_type', 1); //  1 表示退款   2 表示退货
        $product = db('order_product_server')->alias('op')
            ->field('op.*, o.repair_id, o.order_price, o.coupon_money')
            ->join('mob_order_server o', 'o.id = op.order_id')
            ->where('op.id', $product_id)
            ->where('o.member_id', $member_id)
            ->find();
        if (!$product) {
            return view('public/error');
        }
        // 如果优惠金额大于0   计算优惠金额分到这个产品的比例
        $coupon_price = 0; // 初始化优惠金额
        if ($product['coupon_money'] > 0) {
            // 产品单价 * 产品数量 / 订单总金额  获得比例
            $proportion = $product['product_price'] * $product['product_num'] / $product['order_price'];
            // 比例乘以优惠券金额 等级 本产品优惠的金额 退款金额需要减去相应的优惠金额
            $coupon_price = $product['coupon_money'] * $proportion;
        }
        $this->assign('coupon_price', $coupon_price);
        $this->assign('product', $product);
        $this->assign('audit_type', $audit_type);

        // 获取退款原因
        $austin_cause = db('austin_cause')->where('type', $audit_type)->where('status', 1)->select();
        $this->assign('austin_cause', $austin_cause);
        return view();
    }

    // 提交申请
    public function save_austin() {
        if (request()->isAjax()) {
            $param = [
                'member_id' => session('member_id'),
                'order_austin_id' => input('param.order_austin_id', 0),
                'order_id' => input('param.order_id'),
                'product_id' => input('param.product_id'),
                'cause' => input('param.cause'),
                'return_money' => input('param.return_money'),
                'msg' => input('param.msg'),
                'type' => input('param.type'),
                'audit_type' => input('param.audit_type'),
                'obj_id' => input('param.obj_id'),
                'img1' => input('param.img1', ''),
                'img2' => input('param.img2', ''),
                'img3' => input('param.img3', '')
            ];
            $data = Order::order_austin($param);
            return json($data);
        } else {
            $id = input('param.id');
            $data = db('order_austin')->where('id', $id)->find();

            //pj170329add多图弄成数组
            if(!is_weixin()){
                $data["img"] = array();
                if($data["img1"]){$data["img"][] = $data["img1"];}
                if($data["img2"]){$data["img"][] = $data["img2"];}
                if($data["img3"]){$data["img"][] = $data["img3"];}
                //unset($data["img1"],$data["img2"],$data["img3"]);
            }

            $this->assign('data', $data);
            return view();
        }
    }

    // 撤销申请
    public function revocation_austin() {
        $id = input('param.id');
        Db::startTrans();
        try {
            Db::table('mob_order_austin')->where('id', $id)->update(['status' => 3]);
            // 撤销之后检查订单是否还有售后  没有的话 需要把订单的售后状态改为0
            $austin = Db::table('mob_order_austin')->where('id', $id)->find();

            // 首先把对应的订单产品状态修改为0
            $models = ['', 'order_product_buy', 'order_product_server'];
            $model = $models[$austin['type']];
            Db::table('mob_' . $model)->where('id', $austin['product_id'])->update(['austin_status' => 0]);

            // 如果不是已取消和已关闭状态的售后数量为0  就将订单售后状态改为0
            $count = Db::table('mob_order_austin')->where('status', 'not in', '3,5')->where('order_id', $austin['order_id'])->count();
            if ($count == 0) {
                $models = ['', 'order_buy', 'order_server'];
                $model = $models[$austin['type']];
                Db::table('mob_' . $model)->where('id', $austin['order_id'])->update(['after_sale_status' => 0]);
            }
            
            Db::commit();
            return json(['code' => 200, 'msg' => 'success']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    // 售后列表
    public function index() {
        return view();
    }

    // 服务单售后列表
    public function get_server_list() {
        $param = [
            'member_id' => session('member_id'),
            'type' => 2
        ];
        $data = Order::order_austin_list($param);
        //dump($data);
        $view = new View();
        //dump($data);
        if ($data['code'] == 200) {
            $view->assign('data', $data['data']['data']);
        } else {
            $view->assign('data', []);
        }
        $tpl = $view->fetch('austin/list_tpl');
        $data['tpl'] = $tpl;
        return json($data);
    }

    // 售后单详情
    public function austin_info() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){
            session('member_id',@intval($_COOKIE["PJ"]));
            $signPackage = [
                'appId' => '',
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => ''
            ];
        }else{
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
        }
        $this->assign('signPackage', $signPackage);

        $id = input('param.id');
        $data = db('order_austin')->where('id', $id)->find();

        //pj170329add多图弄成数组
        if(!is_weixin()){
            $data["img"] = array();
            if($data["img1"]){$data["img"][] = $data["img1"];}
            if($data["img2"]){$data["img"][] = $data["img2"];}
            if($data["img3"]){$data["img"][] = $data["img3"];}
            //unset($data["img1"],$data["img2"],$data["img3"]);
        }

        if ($data['type'] == 2) {
            $data['order_sn'] = db('order_server')->where('id', $data['order_id'])->value('order_sn');
        }

        $this->assign('data', $data);
        return view();
    }

    // 申诉页面
    public function complaint() {
        $id = input('param.id');
        $data = db('order_austin')->where('id', $id)->find();
        $this->assign('data', $data);
        $models = ['', 'order_buy', 'order_server'];
        $model = $models[$data['type']];
        $order = db($model)->field('order_sn')->where('id', $data['order_id'])->find();
        $this->assign('order', $order);

        // 获取投诉原因
        $austin_cause = db('austin_cause')->where('type', 3)->where('status', 1)->select();
        $this->assign('austin_cause', $austin_cause);
        return view();
    }

    // post 申诉提交
    public function save_complaint() {
        $id = input('param.id');
        $param = [
            'audit_result_msg' => input('param.audit_result_msg'),
            'audit_result_cause' => input('param.audit_result_cause'),
            'status' => 4,
            'audit_result_time' => time()
        ];
        try {
            db('order_austin')->where('id', $id)->update($param);
            return json(['code' => 200, 'msg' => 'success']);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    // 申诉页面列表
    public function complaint_index() {
        return view();
    }

    // 获取申诉列表
    public function get_complaint_list() {
        $param = [
            'member_id' => session('member_id'),
            'type' => 2
        ];
        $data = Order::order_complaint_list($param);
        //dump($data);
        $view = new View();
        //dump($data);
        if ($data['code'] == 200) {
            $view->assign('data', $data['data']['data']);
        } else {
            $view->assign('data', []);
        }
        $tpl = $view->fetch('austin/complaint_list_tpl');
        $data['tpl'] = $tpl;
        return json($data);
    }

    // 申诉详情
    public function complaint_info() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){
            session('member_id',@intval($_COOKIE["PJ"]));
            $signPackage = [
                'appId' => '',
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => ''
            ];
        }else{
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
        }
        $this->assign('signPackage', $signPackage);

        $id = input('param.id');
        $data = db('order_austin')->where('id', $id)->find();

        //pj170329add多图弄成数组
        if(!is_weixin()){
            $data["img"] = array();
            if($data["img1"]){$data["img"][] = $data["img1"];}
            if($data["img2"]){$data["img"][] = $data["img2"];}
            if($data["img3"]){$data["img"][] = $data["img3"];}
            //unset($data["img1"],$data["img2"],$data["img3"]);
        }

        $this->assign('data', $data);
        return view();
    }
}
